/**
 * 
 * The MIT License
 * 
 * Copyright (c) 2008 the original author or authors.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.googlecode.gchartjava;

import static com.googlecode.gchartjava.ArraysUtil.asUnmodifiableList;
import static java.util.Collections.unmodifiableList;

import java.util.List;

/**
 * A Google-o-meter looks a bit like a speedometer dial. The range is from 0 to
 * a 100. Colors are interpolated from red to green by default. You can provide
 * your own color gradient.
 * 
 * @author Julien Chastang (julien.c.chastang at gmail dot com)
 */
public class GoogleOMeter extends AbstractGChart {
	private static final String GOOGLE_O_METER_URL = GCHART_URL + "?cht=gom";
	private static final String LABEL_PARAM        = "chl=";
	
	private final float data;
	private List<Color> colors;
	private String label;
	
	/**
	 * Create a GoogleOMeter.
	 * 
	 * @param data
	 *            A number between 0 and 100.
	 */
	public GoogleOMeter(final float data) {
		super();
		this.data = data;
	}
	
	/**
	 * Create a GoogleOMeter.
	 * 
	 * @param data
	 *            A number between 0 and 100.
	 * @param label
	 *            Label that will appear at the top of the arrow.
	 * @param colors
	 *            Must provide at least two color. Colors will be interpolated
	 *            between colors provided.
	 * @see Color
	 */
	public GoogleOMeter(final float data, final String label, final Color...colors) {
		super();
		this.data = data;
		this.label = label;
		if (colors == null || colors.length < 2)
			throw new IllegalArgumentException("Must provide at least 2 colors.");
		this.colors = asUnmodifiableList(colors);
	}
	
	/**
	 * Create a GoogleOMeter.
	 * 
	 * @param data
	 *            A number between 0 and 100.
	 * @param label
	 *            Label that will appear at the top of the arrow.
	 * @param colors
	 *            Must provide at least two color. Colors will be interpolated
	 *            between colors provided.
	 * @see Color
	 */
	public GoogleOMeter(final float data, final String label, final List<? extends Color> colors) {
		super();
		this.data = data;
		this.label = label;
		if (colors == null || colors.size() < 2)
			throw new IllegalArgumentException("Must provide at least 2 colors.");
		this.colors = unmodifiableList(colors);
	}	

	/**
	 *
	 */
	@Override
	protected void prepareData() {
		setData(asUnmodifiableList(new Data(data)));
		if (colors != null)
			setColors(unmodifiableList(colors));
		if (label != null)
			addParameter(LABEL_PARAM, label);
	}

	/**
	 *
	 */
	@Override
	protected String getBaseURLString() {
		return GOOGLE_O_METER_URL;
	}
}
